class MessagesController < ApplicationController
  def index
    
  end

  def new
    @message = Message.new
    respond_to do |format|
      format.js
    end
  end

  def create
    @message = Message.new(params[:message])
    respond_to do|format|
      if @message.news
        @message.recipient = 0
      end
      if @message.save
        format.js
        format.html{
          flash[:notice] = "Wiadomość wysłana pomyślnie"
          redirect_to :controller => 'users'
        }
      end
    end
    rescue ActiveRecord::RecordInvalid => e
      respond_to do |format|
        format.js {
          render :update do |page|
            page.replace_html :message_errors, e.message[19..-1]
            page.visual_effect :highlight, :message_errors, :duration => 2,
             :startcolor => "#ff0000", :endcolor => "#FFFF00"
          end   }
      end
  end

  def edit
    @message = Message.find(params[:id])
  end
  
  def update
    @message = Message.find(params[:id])
    if @message.update_attributes(params[:message])
      flash[:notice] = "Successfully updated messages."
      redirect_to messages_url
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    @message = Message.find(params[:id])
    @message.destroy
    respond_to do |format|
      format.js
      format.html{
        flash[:notice] = "Successfully destroyed message."
        redirect_to :controller => 'admin', :action => 'messages' }
    end
  end

  def mark_as_read
    @message = Message.find(params[:id])
    @message.read = true;
    @message.save
    respond_to do |format|
      format.js
    end
    flash[:notice] = "Oznaczono jako przeczytaną."

  end
  
end
